﻿Imports System.IO

Public Class FormularioTratamientos
    Private Sub FormularioTratamientos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ', MyBase.VisibleChanged

        labEmpleado.Text = Sesion.getEmpleadoActivo().getNombre()
        If Sesion.getEmpleadoActivo().esAdmin Then
            gbAdministracion.Visible = True
        Else
            gbAdministracion.Visible = False
        End If
        Dim ctl As Control = tcServicios
        montarTexto()
    End Sub

#Region "interfaz"
    'Montamos los textos de la interfaz, obteniéndolos de los corresponcientes archivos.
    Public Sub montarTexto()
        Dim control As Control = tcServicios
        Dim gb As Control = flpManicura
        While Not control Is Nothing
            'Comprobamos si es uno de los FlowLayoutPanel que contiene los botones
            If TypeOf control Is FlowLayoutPanel And Not control.Name = "flpDepilacion" Then
                getNombresServicios(control)
            End If
            control = Me.GetNextControl(control, True)
        End While
    End Sub

    Public Function getRutaArchivoAsociado(ByVal flp As FlowLayoutPanel) As String
        Select Case flp.Name
            Case "flpManicura"
                Return "c:/eleemeTPV/serviciosManicura.txt"
            Case "flpPedicura"
                Return "c:/eleemeTPV/serviciosPedicura.txt"
            Case "flpMaquillaje"
                Return "c:/eleemeTPV/serviciosMaquillaje.txt"
            Case "flpFria"
                Return "c:/eleemeTPV/serviciosFria.txt"
            Case "flpCaliente"
                Return "c:/eleemeTPV/serviciosCaliente.txt"
            Case "flpIPL"
                Return "c:/eleemeTPV/serviciosILP.txt"
            Case "flpFacial"
                Return "c:/eleemeTPV/serviciosFacial.txt"
            Case "flpCorporal"
                Return "c:/eleemeTPV/serviciosCorporal.txt"
            Case Else
                Return Nothing
        End Select
    End Function
    Public Sub setTFP(ByVal seccion As FlowLayoutPanel)
        Dim reader = My.Computer.FileSystem.OpenTextFieldParser(getRutaArchivoAsociado(seccion))
        reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
        reader.Delimiters = delimitadores
    End Sub

    Public Sub getNombresServicios(ByRef seccion As FlowLayoutPanel) 'As String
        FileOpen(1, getRutaArchivoAsociado(seccion), OpenMode.Input)
        Dim linea As String = ""
        Dim campos() As String
        Dim gb As Control = Me.GetNextControl(seccion, True)
        Dim flag As Boolean = True
        Dim servicio As Servicio
        While Not gb Is Nothing

            If TypeOf gb Is GroupBox And seccion.Contains(gb) Then
                Input(1, linea)
                campos = Split(linea, ";")
                Try
                    servicio = New Servicio(campos(0), _
                                            Integer.Parse(campos(2)), _
                                            Integer.Parse(campos(4)), _
                                            Integer.Parse(campos(5)))
                Catch e As Exception
                    MsgBox("errooooor" + e.Message) 'esto peta si uso Servicio, porque no puedo parsear las strings
                    FileClose(1)
                    Me.Close()
                End Try


                gb.Text = servicio.getNombre()
                For Each boton As Button In gb.Controls
                    boton.TextAlign = ContentAlignment.MiddleLeft
                    If flag Then
                        AddHandler boton.Click, AddressOf handlerBotonesServicio
                        boton.Text = servicio.getPrecio() & " €"
                        boton.BackgroundImage = Global.WindowsApplication1.My.Resources.Resources.comprar2

                    Else
                        If servicio.getSesionesBono <> "0" Then
                            AddHandler boton.Click, AddressOf handlerBotonesBono
                            boton.Text = "BONO " & servicio.getSesionesBono & vbCrLf & servicio.getPrecioBono & " €"

                            boton.BackgroundImage = Global.WindowsApplication1.My.Resources.Resources.comprarBono
                        Else
                            boton.Visible = False
                        End If
                    End If
                    flag = Not flag
                Next
            End If
            gb = Me.GetNextControl(gb, True)
        End While
        FileClose(1)
    End Sub


#End Region

#Region "Handlers botones"
    Private Function getGroupBox(ByRef boton As Button) As GroupBox
        Dim gb As Control = boton
        While Not TypeOf gb Is GroupBox
            gb = Me.GetNextControl(gb, False)
        End While
        Return gb
    End Function

    Private Function getFLP(ByRef gb As GroupBox) As FlowLayoutPanel
        Dim flp As Control = gb
        While Not TypeOf flp Is FlowLayoutPanel
            flp = Me.GetNextControl(flp, False)
        End While
        Return flp
    End Function



    Private Sub handlerBotonesServicio(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim gb As GroupBox = getGroupBox(sender)
        FileOpen(1, getRutaArchivoAsociado(getFLP(gb)), OpenMode.Input)
        Dim nombreServicio As String = gb.Text
        Dim linea As String = "bla"
        Dim campos() As String = {"bla", "bla", "bla", "bla", "bla", "bla"}
        While Not nombreServicio = campos(0)
            Input(1, linea)
            campos = Split(linea, ";")
        End While
        If Not campos(0) = "bla" Then
            lbTicket.Items.Add(campos(0))
            lbPrecio.Items.Add(RSet(campos(2), 3))
            tbTotal.Text = Integer.Parse(tbTotal.Text) + Integer.Parse(campos(2))
            'sumar a caja 
        Else
            MsgBox("Error lectura fichero servicios")
        End If
        FileClose(1)
    End Sub

    Private Sub handlerBotonesBono(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim gb As GroupBox = getGroupBox(sender)
        FileOpen(1, getRutaArchivoAsociado(getFLP(gb)), OpenMode.Input)
        Dim nombreServicio As String = gb.Text
        Dim linea As String = "bla"
        Dim campos() As String = {"bla", "bla", "bla", "bla", "bla", "bla"}
        While Not nombreServicio = campos(0)
            Input(1, linea)
            campos = Split(linea, ";")
        End While
        If Not campos(0) = "bla" Then
            lbTicket.Items.Add("Bono " & campos(4) & " " & campos(0))
            lbPrecio.Items.Add(RSet(campos(5), 3))
            tbTotal.Text = Integer.Parse(tbTotal.Text) + Integer.Parse(campos(5))
            'sumar a caja 
        Else
            MsgBox("Error lectura fichero servicios")
        End If
        FileClose(1)
    End Sub
#End Region








    Private Sub botonCerrarSesion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles botonCerrarSesion.Click
        'comprobar si hay una venta activa
        If lbPrecio.Text <> "0" Then
            MsgBox("Por favor, finalice la venta o cancélela antes de cambiar de usuario.")
        Else
            FormularioLogeo.txbPassword.Clear()
            FormularioLogeo.txbUsuario.Clear()

            FormularioLogeo.Visible = True
            Me.Close()
        End If
    End Sub

    Private Sub botonSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles botonSalir.Click
        Me.Close()
        FormularioLogeo.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        FormularioConfirmarTicket.Visible = True
        FormularioConfirmarTicket.ppcTicket.Document = FormularioConfirmarTicket.ticket
    End Sub

    
    Private Sub eliminarServicio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminarServicio.Click
        If Not lbTicket.SelectedItem Is Nothing Then
            Dim indices = lbTicket.SelectedIndices
            Dim n As Integer = indices.Count - 1
            Dim eliminar = MsgBox("Ha seleccionado " & n + 1 & " elemento(s) ¿Desea eliminarlo(s) del ticket?", MsgBoxStyle.OkCancel)
            If eliminar = MsgBoxResult.Ok Then

                For i As Integer = n To 0 Step -1
                    Dim j As Integer = indices(i)
                    tbTotal.Text = Integer.Parse(tbTotal.Text) - Integer.Parse(lbPrecio.Items(j))
                    lbTicket.Items.RemoveAt(j)
                    lbPrecio.Items.RemoveAt(j)

                Next
            End If
        Else
            MsgBox("Seleccione el elemento de la lista que quiera eliminar")
        End If
    End Sub

    Private Sub botonEditarUsuarios_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles botonEditarUsuarios.Click
        FormMenuUsuarios.Show()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim eliminar = MsgBox("Desea borrar todo el contenido del ticket?", MsgBoxStyle.OkCancel)
        If eliminar = MsgBoxResult.Ok Then
            lbTicket.Items.Clear()
            lbPrecio.Items.Clear()
            tbTotal.Text = "0"
        End If
    End Sub

    Private Sub Button141_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button141.Click
        FormularioCaja.Show()

    End Sub
End Class